<template>
  <div id="globalHeader">
    <!--全局顶部栏-->
    <a-row :wrap="false">

      <a-col flex="200px">
        <router-link to="/">
          <div class="title-bar">
            <img  class="logo" src="../assets/logo.png" alt="logo">
            <div class="title">
              &nbsp;&nbsp;&nbsp;智能云图库
            </div>
          </div>
        </router-link>
      </a-col>

      <a-col flex="auto">
        <a-menu v-model:selectedKeys="current"
                mode="horizontal"
                :items="items"
                @click="doMenuClick"
        />
      </a-col>

      <a-col flex="100px">
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id">
            <a-dropdown>
              <ASpace>
                <a-avatar src="loginUserStore.loginUser.userAvatar" />
                {{ loginUserStore.loginUser.userName ?? '无名' }}
              </ASpace>
              <template #overlay>
                <a-menu>
                  <a-menu-item @click="doLogout">
                    <LogoutOutlined style="color: red"/>
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>

  </div>

</template>

<script lang="ts" setup>
import { useLoginUserStore } from '@/stores/user'


const loginUserStore = useLoginUserStore()

import { computed, h, ref } from 'vue'
import { HomeOutlined, LogoutOutlined } from '@ant-design/icons-vue'
import { type MenuProps, message } from 'ant-design-vue'

const originItems = [
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/admin/userManage',
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/add_picture',
    label: '创建图片',
    title: '创建图片',
  },
  {
    key: '/admin/pictureManage',
    label: '图片管理',
    title: '图片管理',
  },
  {
    key: 'others',
    label: h('a', { href: 'https://www.codefather.cn', target: '_blank' }, '编程导航'),
    title: '编程导航',
  },
]

import { useRouter } from 'vue-router'
import { userLogoutUsingPost } from '@/api/userController'
const router = useRouter();
// 路由跳转事件
const doMenuClick = ({ key } : { key: string}) => {
  router.push({
    path: key
  });
};

const current = ref<string[]>(['home']) // 当前选中的菜单
// 监听路由变化，更新当前选中菜单
router.afterEach((to) => {
  current.value = [to.path];
})

// 用户注销
const doLogout = async () => {
  const res = await userLogoutUsingPost()
  console.log(res)
  if (res.data.code === 0) {
    loginUserStore.setLoginUser({
      userName: '未登录',
    })
    message.success('退出登录成功')
    await router.push('/user/login')
  }else {
    message.error('退出登录失败' + res.data.message)
  }
}


// 过滤菜单项
const filterMenus = (menus = [] as MenuProps['items']) => {
  return menus?.filter((menu) => {
    if (menu.key.startsWith('/admin')) {
      const loginUser = loginUserStore.loginUser
      if (!loginUser || loginUser.userRole !== "admin") {
        return false
      }
    }
    return true
  })
}

const items = computed<MenuProps['items']>(() => filterMenus(originItems))

</script>

<style  scoped>
.title-bar{
  display: flex;
  align-items: center;
}
.title{
  color: black;
  font-size: 18px;
}
.logo{
  height: 50px;
}
</style>

